import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class CustomDoubleTapHelp {
  final GestureTapCallback? onDoubleTap;

  CustomDoubleTapHelp({this.onDoubleTap});

  final Stopwatch _stopwatch = Stopwatch();
  bool _isWaitingForDoubleTap = false;

  //使用单击自己实现双击
  void callOnTap() {
    if (!_stopwatch.isRunning) {
      _stopwatch.start();
      _isWaitingForDoubleTap = true;

      Future.delayed(const Duration(milliseconds: 300), () {
        if (_isWaitingForDoubleTap) {
          _isWaitingForDoubleTap = false;
          _stopwatch.reset();
        }
      });
    } else {
      if (_stopwatch.elapsedMilliseconds < 300) {
        _isWaitingForDoubleTap = false;
        //回调双击事件
        onDoubleTap?.call();
      }
      _stopwatch.reset();
    }
  }
}
